06. DockerHub

DockerHub pre-concepts

DockerHub

DockerHub is the world’s largest registry of Docker images with more than 100,000 images available. DockerHub is the default registry for Docker. It contains images ready to run a great variety of applications.

In the next video, you will walk through the steps of pulling an image from DockerHub and running the image on your own computer.

DockerHub Exercise

FSND C4 L1 A06 DockerHub

Downloading and running an image from DockerHub

DockerHub Exercise Instructions

  1. Find the Postgres image by searching on DockerHub , or use this link to go directly to the image page. Postgres is an open source relational database, and the Postgres Docker image has the database software installed.
  2. On the command line tell docker to download the image to your machine:
docker pull postgres:latest
  1. Run the image using the docker run command, supplying a password for Postgres:
docker run --name psql -e POSTGRES_PASSWORD=password! -p 5432:5432 -d postgres:latest

In the command above:

  • The --name flag allows you to specify a name for the container that can be used later to reference the container. If you don’t specify a name, Docker will assign a random string name to the container.
  • The -e flag stands for “environment”. This sets the environment variable POSTGRES_PASSWORD to the value password! .
  • The -p flag stands for “publish”. This allows you to bind your local machine’s port 5432 to the container port 5432.
  • The -d stands for “detach”. This tells Docker run the indicated image in the background and print the container ID. When you use this command, you will still be able to use the terminal to run other commands, otherwise you would need to open a new terminal.
  1. Download and install the Postgres client .
  2. Once you have the Postgres client installed, you can use it to connect with the Docker container database using the following command:
psql -h 0.0.0.0 -p 5432 -U postgres

This command allows you to access the database using the same port that you exposed earlier. Note that after running that command you will need to enter the same password that you set with the POSTGRES_PASSWORD when creating the container ( password! ).

  1. Test the container with some SQL commands. For example, you can list all databases using \l command or list all tables using the \dt command. More commands can be found in the Postgres documentation here .
  2. When you are finished testing Postgres, you can quit your connection to postgres using \q
  3. To stop the running Docker container, you will first need to find it. You can list the running containers with the command docker ps . Copy the id of your postgres container.
  4. Use this id to stop the container:
docker stop <container_id>

DockerHub Checkpoint

Task Description:

Follow the steps above to download, run, and test the Postgres Docker image.

Task List:

Task Feedback:

Excellent work! You now have some experience working with a Docker image and running container.

DockerHub Quizzes Header

DockerHub Quizzes

Docker Registry

DockerHub is a

SOLUTION: Docker Registry

Docker Run Flags

When used with a docker run command, the -e flag allows you to:

SOLUTION:
  • Specify environment variables for the Docker container.

Summary and Further Research

Summary

DockerHub is the largest and most popular Docker image registry, and in this classroom concept, you have gained some experience working with a DockerHub image and container. You downloaded and ran the Docker Postgres image, and you were able to test that the database was working by listing the databases in the container.

On DockerHub, you will find images designed to run many different applications and languages. These images can be used as they are, or as you shall see in the next section, as the base for designing your own images.

Further Research

You can explore popular Docker images by clicking on the “Browse Popular Images” link on this page.